package com.example.translationapp.activity;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.translationapp.R;
import com.example.translationapp.model.TransLationBean;
import com.example.translationapp.viewmodel.TranslationModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 翻译界面
 */
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_clear)
    TextView tvClear;
    @BindView(R.id.input)
    EditText input;
    @BindView(R.id.bt_trans)
    Button btTrans;
    @BindView(R.id.tv_result)
    TextView tvResult;
    @BindView(R.id.iv_faver)
    ImageView ivFaver;

    private TranslationModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        model = ViewModelProviders.of(this).get(TranslationModel.class);
        model.getTranslation().observe(this, new Observer<TransLationBean>() {
            @Override
            public void onChanged(@Nullable TransLationBean transLationBean) {
                Log.setText(transLationBean.trans_result.get(0).dst);
            }
        });

        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_clear, R.id.bt_trans, R.id.iv_faver})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_faver:
                model.save(tvResult.getText().toString());
                break;
            case R.id.tv_clear:
                input.setText("");
                break;
            case R.id.bt_trans:
                model.loadTransLation(input.getText().toString());
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent(MainActivity.this, ListActivity.class);
        startActivity(intent);
        return super.onOptionsItemSelected(item);
    }

}
